package net.mingsoft.basic.util;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.math.BigDecimal;


/**
 * @author gengfa
 */
public class DateUtil {

    public static final String DATE_FORMAT_STR = "yyyy-MM-dd";
    public static final String DATE_FORMAT_STR_L = "yyyyMMdd";
    public static final String DATE_FORMAT_YEAR_MONTH = "yyyyMM";
    public static final String DATE_FORMAT_YEAR__MONTH = "yyyy-MM";
    public static final String DATE_FORMAT_MONTH_DAY_CN = "MM月dd日";
    public static final String DATE_FORMAT_YEAR_MONTH_DAY_CN = "yyyy年MM月dd日";
    private static final String SPACE = " ";
    private static final String TIME_STR = "00:00:00";
    private static final String TIME_STR_END = "23:59:59";
    public static final String DATETIME_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT_6_STR = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final String DATETIME_FORMAT_4_FILE = "yyyyMMdd_HHmmss";
    public static final String DATETIME_FORMAT_WH = "yyyy-MM-dd\\THH:mm:ss";

    private static final BigDecimal PER_DAY_DECIMAL = new BigDecimal((1000 * 3600 * 24));

    private DateUtil() {
    }

    public static String transferDateFormat(String source, String sourceFormat, String targetFormat) {
        return DateTime.parse(source, DateTimeFormat.forPattern(sourceFormat)).toString(targetFormat);
    }

    public static long days(DateTime begin, DateTime end) {
        return new BigDecimal((end.getMillis() - begin.getMillis()))
                .divide(PER_DAY_DECIMAL, BigDecimal.ROUND_HALF_UP)
                .intValue();
    }

    public static String dateBegin(String date) {
        return DateTime.parse(date).toString(DATE_FORMAT_STR) + SPACE + TIME_STR;
    }

    public static String endBegin(String date) {
        return DateTime.parse(date).toString(DATE_FORMAT_STR) + SPACE + TIME_STR_END;
    }

    public static String todayBegin() {
        return DateTime.now().toString(DATE_FORMAT_STR) + SPACE + TIME_STR;
    }

    public static String todayEnd() {
        return DateTime.now().toString(DATE_FORMAT_STR) + SPACE + TIME_STR_END;
    }

    public static String lastNDay(int n) {
        return DateTime.now().minusDays(n).toString(DATE_FORMAT_STR) + SPACE + TIME_STR;
    }

    public static String plus(String date, int n) {
        return DateTime
                .parse(date, DateTimeFormat.forPattern(DATETIME_FORMAT_STR))
                .plusSeconds(n)
                .toString(DATETIME_FORMAT_STR);
    }

    public static String now() {
        DateTime dt = DateTime.now();
        return dt.toString(DATETIME_FORMAT_STR);
    }

    public static String nowZhCN() {
        DateTime dt = DateTime.now();
        return dt.toString(DATE_FORMAT_YEAR_MONTH_DAY_CN);
    }

    public static String nowZhCNByDateString(String date) {
        DateTime dt = DateTime.parse(date, DateTimeFormat.forPattern(DATE_FORMAT_STR));
        return dt.toString(DATE_FORMAT_YEAR_MONTH_DAY_CN);
    }

    public static String now6() {
        DateTime dt = DateTime.now();
        return dt.toString(DATETIME_FORMAT_6_STR);
    }

    public static String nowDate() {
        DateTime dt = DateTime.now();
        return dt.toString(DATE_FORMAT_STR);
    }

    public static long nowMills() {
        return DateTime.now().getMillis();
    }

    public static String nowDateWithoutL() {
        DateTime dt = DateTime.now();
        return dt.toString(DATE_FORMAT_STR_L);
    }

    public static String mills() {
        return String.valueOf(DateTime.now().getMillis());
    }

    public static String now4f() {
        DateTime dt = DateTime.now();
        return dt.toString(DATETIME_FORMAT_4_FILE);
    }

    public static boolean after(String a, String b) {
        return toJodaDateTime(a).isAfter(toJodaDateTime(b));
    }

    public static boolean afterR(String a, String b) {
        return toJodaDateTimeR(a).isAfter(toJodaDateTimeR(b));
    }

    public static boolean afterEq(String a, String b) {
        DateTime d1 = toJodaDateTimeR(a);
        DateTime d2 = toJodaDateTimeR(b);
        return d1.isEqual(d2) || d1.isAfter(d2);
    }

    public static boolean equal(String a, String b) {
        return toJodaDateTime(a).equals(toJodaDateTime(b));
    }

    public static boolean equalR(String a, String b) {
        return toJodaDateTimeR(a).equals(toJodaDateTimeR(b));
    }

    public static DateTime toJodaDateTime(String timeStr) {
        return DateTime.parse(timeStr, DateTimeFormat.forPattern(DATETIME_FORMAT_STR));
    }

    public static DateTime toJodaDateTimeR(String timeStr) {
        return DateTime.parse(timeStr, DateTimeFormat.forPattern(DATE_FORMAT_STR));
    }

    public static String toJodaDateTimeString(String timeStr, String pattern) {
        DateTime curr = DateTime.parse(timeStr, DateTimeFormat.forPattern(pattern));
        return curr.toString(DATETIME_FORMAT_STR);
    }

    public static String replaceT(String timeStr) {
        return timeStr.replace("T", " ");
    }
}
